package wypozyczalnia.dao;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;

import org.hibernate.Hibernate;
import org.hibernate.SessionFactory;
import org.hibernate.jdbc.Work;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import wypozyczalnia.domain.Movie;
import wypozyczalnia.domain.Person;

@Repository
public class PersonDao extends GenericDao<Person>{

	@Autowired
	public PersonDao(SessionFactory sf) {
		super(Person.class, sf);
	}

	public Person getPersonById(Integer id) {
		Person person = (Person) getById(id);
		Hibernate.initialize(person.getPhotos());
		Hibernate.initialize(person.getMovie());
		Hibernate.initialize(person.getHires());
		return person;
	}
	
	public void deletePerson(final Integer id){
		this.getSession().doWork(new Work() {

			public void execute(Connection conn) {
				CallableStatement stmt = null;
				try {
					stmt = conn.prepareCall("call DELETE_PERSON(?)");
				} catch (SQLException e1) {

					e1.printStackTrace();
				}
				try {
					stmt.setInt(1, id.intValue());

				} catch (SQLException e) {

					e.printStackTrace();
				}
				try {
					stmt.execute();
				} catch (SQLException e) {

					e.printStackTrace();
				}
			}
		});
	

}
